Imports System.Windows.Forms

Imports Vintasoft.Imaging.Codecs

Namespace Imaging.Codecs.Dialogs
	Public Partial Class PngEncoderSettingsForm
		Inherits Form

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()

			filterMethodComboBox.Items.Add(PngFilterMethod.None)
			filterMethodComboBox.Items.Add(PngFilterMethod.Auto)
			filterMethodComboBox.Items.Add(PngFilterMethod.[Sub])
			filterMethodComboBox.Items.Add(PngFilterMethod.Up)
			filterMethodComboBox.Items.Add(PngFilterMethod.Average)
			filterMethodComboBox.Items.Add(PngFilterMethod.Paeth)
			filterMethodComboBox.Items.Add(PngFilterMethod.Adaptive)

			For i As Integer = 0 To 9
				compressionLevelComboBox.Items.Add(i)
			Next
		End Sub

		#End Region



		#Region "Properties"


		Private _encoderSettings As PngEncoderSettings
		''' <summary>
		''' Gets or sets PNG encoder settings.
		''' </summary>
		Public Property EncoderSettings() As PngEncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					Throw New ArgumentOutOfRangeException()
				End If
				If _encoderSettings IsNot value Then
					_encoderSettings = value
					ShowEncoderSettings()
				End If
			End Set
		End Property


		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)

			If EncoderSettings Is Nothing Then
				EncoderSettings = PngEncoderSettings.Fast
				fastRadioButton.Checked = True
			End If
		End Sub

		Private Sub ShowEncoderSettings()
			If EncoderSettings.Equals(PngEncoderSettings.BestSpeed) Then
				bestSpeedRadioButton.Checked = True
			ElseIf EncoderSettings.Equals(PngEncoderSettings.Fast) Then
				fastRadioButton.Checked = True
			ElseIf EncoderSettings.Equals(PngEncoderSettings.Normal) Then
				normalRadioButton.Checked = True
			ElseIf EncoderSettings.Equals(PngEncoderSettings.BestCompression) Then
				bestCompressionRadioButton.Checked = True
			Else
				customRadioButton.Checked = True
			End If
			customGroupBox.Enabled = customRadioButton.Checked
			filterMethodComboBox.SelectedItem = EncoderSettings.FilterMethod
			compressionLevelComboBox.SelectedItem = EncoderSettings.CompressionLevel
		End Sub

		Private Sub SetEncoderSettings()
			EncoderSettings = New PngEncoderSettings(DirectCast(filterMethodComboBox.SelectedItem, PngFilterMethod), CInt(compressionLevelComboBox.SelectedItem))
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetEncoderSettings()

			DialogResult = DialogResult.OK
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub bestSpeedRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If bestSpeedRadioButton.Checked Then
				EncoderSettings = PngEncoderSettings.BestSpeed
			End If
		End Sub

		Private Sub fastRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If fastRadioButton.Checked Then
				EncoderSettings = PngEncoderSettings.Fast
			End If
		End Sub

		Private Sub normalRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If normalRadioButton.Checked Then
				EncoderSettings = PngEncoderSettings.Normal
			End If
		End Sub

		Private Sub bestCompressionRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If bestCompressionRadioButton.Checked Then
				EncoderSettings = PngEncoderSettings.BestCompression
			End If
		End Sub

		Private Sub customRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If customRadioButton.Checked Then
				customGroupBox.Enabled = True
			End If
		End Sub

		#End Region

	End Class
End Namespace
